//***************************************** A j a x W o r k e r   C L A S S *************************************************************************
var serverDomain = "http://localhost:8000/";
var SERVER_ACTION_SEARCH = "json/search/";
var SERVER_ACTION_CONTENT = "json/content";
var SERVER_ACTION_KEYWORD = "json/keyword/";

function AJAXWorker() {

    function getXMLHttpReqObject() {
        var xml_http_req = null;
        if (window.XMLHttpRequest)
            xml_http_req = new XMLHttpRequest();

        return xml_http_req;
    }


    this.sendContentRequest = function(Content) {
        var gui_Handler = new GuiHandler();
        var server = serverDomain + SERVER_ACTION_CONTENT; 
        var xml_http_req = getXMLHttpReqObject();
        xml_http_req.onreadystatechange = function() {
            if (xml_http_req.readyState == 4) {
                if (xml_http_req.status == 200) {
                    content_response = jsonToObject(xml_http_req.responseText);
                    IDClient = content_response.id;
                    (new AJAXWorker).getKeywordsRequest();
                    (new AJAXWorker).getReferences();
                }
//                else
//                    throw new Error("An error occurred on attempt to send content! XMLHttpRequest object returnd status: " + xml_http_req.status);
            }
        };
        xml_http_req.open("POST", server, true);
        xml_http_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        xml_http_req.send(Content);
    };



    this.getReferences = function() {
        var server = serverDomain + SERVER_ACTION_SEARCH + IDClient; // server's address
        var xmlHttpReq = getXMLHttpReqObject();
        xmlHttpReq.onreadystatechange = function() {
            if (xmlHttpReq.readyState == 4) {
                if (xmlHttpReq.status == 200) {
                    Discovery = jsonToObject(xmlHttpReq.responseText);
                    var gui_Handler = new GuiHandler();
                    gui_Handler.showSearch();
                }
//                else
//                    throw new Error("An error occurred on attempt to get relevant links! XMLHttpRequest object returned status: " + xml_http_req.status);
            }
        };
        xmlHttpReq.open("GET", server, true);
        xmlHttpReq.setRequestHeader('Content-Type',
			'application/x-www-form-urlencoded; charset=UTF-8');
        xmlHttpReq.send(objectToJson(IDClient));
   };




    this.getKeywordsRequest = function() {
        var server = serverDomain + SERVER_ACTION_KEYWORD + IDClient; // server's address
        var xml_http_req = getXMLHttpReqObject();
        xml_http_req.onreadystatechange = function() {
            if (xml_http_req.readyState == 4) {
                if (xml_http_req.status == 200) {
                    Keywords = jsonToObject(xml_http_req.responseText);
                    var gui_Handler = new GuiHandler();
                    gui_Handler.showKeywords();
                }
//                else
//                    throw new Error("An error occurred on attempt to get keywords! XMLHttpRequest object returned status: " + xml_http_req.status);
            }
        };
        xml_http_req.open("GET", server, true);
        xml_http_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        var message = objectToJson(IDClient);
        xml_http_req.send(message);
    };



    this.sendCUDKeywordRequest = function(method, data) {
        var server =serverDomain + "json/keyword"; // server's address
        var xml_http_req = getXMLHttpReqObject();
        xml_http_req.open(method, server, true);
        xml_http_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        xml_http_req.send(data);
    };

}
